from watermark_build.code.main.config.template_config import *
from PIL import Image, ImageDraw, ImageFilter

def read_image(path):
    try:
        with open(path, 'rb') as file:
            content = file.read()
            print(f"File content: {content[:10]}... (first 10 bytes)")
            return content
    except FileNotFoundError:
        print(f"Error: File not found at path '{path}'")
        return None
    except Exception as e:
        print(f"Error reading file: {e}")
        return None


def create_white_image(width, height, output_path):
    """
    创建一张白底的照片，并保存到指定路径。

    :param width: 图像宽度
    :param height: 图像高度
    :param output_path: 输出文件路径
    """
    # 创建一个白色背景的图像
    image = Image.new('RGB', (width, height), color='white')

    # 保存图像
    image.save(output_path)
    print(f"Image created and saved to: {output_path}")


if __name__ == '__main__':
    # 打开图片
    original_image = Image.open(UPLOAD_PATH)

    # 获取图片大小
    width, height = original_image.size
    background_width = width + WIDTH_MARGIN
    background_height = height + HEIGHT_MARGIN
    # 创建一个白色的背景图片
    background = Image.new('RGB', (background_width, background_height), 'white')

    # 创建圆形蒙版
    radius = 150  # 圆角半径
    mask = Image.new('L', original_image.size, 0)
    draw = ImageDraw.Draw(mask)
    draw.rounded_rectangle((0, 0, original_image.width, original_image.height), radius, fill=255)

    # 应用圆形蒙版到原始图片上
    rounded_image = Image.composite(original_image.convert('RGBA'),
                                    Image.new('RGBA', original_image.size, (0, 0, 0, 0)), mask)

    # 添加阴影效果
    shadow_offset = (100, 100)  # 阴影偏移量
    shadow_blur = 10  # 阴影模糊程度
    shadow_color = (0, 0, 0, 50)  # 阴影颜色和透明度

    # 创建阴影图片
    shadow_image = Image.new('RGBA', rounded_image.size, (0, 0, 0, 0))
    draw = ImageDraw.Draw(shadow_image)
    draw.rounded_rectangle((0, 0, rounded_image.width, rounded_image.height), radius, fill=shadow_color)

    # 应用高斯模糊
    shadow_image = shadow_image.filter(ImageFilter.GaussianBlur(shadow_blur))

    # 计算粘贴位置
    paste_position = (
        MARGIN,
        MARGIN
    )

    shadow_paste_position = (
        MARGIN + shadow_offset[0],
        MARGIN + shadow_offset[1]
    )
    # 将阴影图片粘贴到背景图片上
    background.paste(shadow_image, shadow_paste_position, mask=shadow_image)

    # 将原始图片粘贴到背景图片上
    # 将处理后的图片粘贴到背景图片上
    background.paste(rounded_image, paste_position, mask=rounded_image)

    # 显示或保存最终图片
    background.show()
    # 或者保存图片
    background.save('output_image.jpg')